package cn.itrip.biz.controller;

import cn.itrip.biz.service.itripHotelOrder.ItripHotelOrderService;
import cn.itrip.biz.service.itripUserLinkUser.ItripUserLinkUserService;
import cn.itrip.common.DateUtil;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.RedisUtils;
import cn.itrip.dto.Dto;
import cn.itrip.pojo.ItripHotelOrder;
import cn.itrip.pojo.ItripUser;
import cn.itrip.pojo.ItripUserLinkUser;
import cn.itrip.vo.userinfo.ItripAddUserLinkUserVO;
import cn.itrip.vo.userinfo.ItripModifyUserLinkUserVO;
import cn.itrip.vo.userinfo.ItripSearchUserLinkUserVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.spi.http.HttpHandler;
import java.util.Date;
import java.util.List;

@RestController  //Controller+responseBody
@RequestMapping(value = "/api/userinfo")
@Api(value = "UserInfoController",tags = "{UserInfoController}")
public class UserInfoController {

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ItripUserLinkUserService itripUserLinkUserService;
    @Resource
    private ItripHotelOrderService itripHotelOrderService;


    /**
     *   修改常用联系人接口
     */
    @ApiOperation(value = "修改常用联系人接口")
    @RequestMapping(value = "/modifyuserlinkuser", method = RequestMethod.POST, produces = "application/json")
    public Dto modifyUserLinkUser(@RequestBody ItripModifyUserLinkUserVO vo, HttpServletRequest request){
        try {

            //1.验证token
            String token = request.getHeader("token");

            if (token==null || !redisUtils.exit(token)){
                return DtoUtil.returnFail("token失效，请重登录", "100000");
            }
            if (EmptyUtils.isEmpty(vo.getLinkIdCard()) || EmptyUtils.isEmpty(vo.getLinkIdCardType()) || EmptyUtils.isEmpty(vo.getLinkUserName())  || EmptyUtils.isEmpty(vo.getId())){
                return DtoUtil.returnFail("不能提交空，请填写常用联系人信息", "100422");
            }

            ItripUser itripUser = JSONObject.parseObject(redisUtils.getValue(token), ItripUser.class);
            ItripUserLinkUser itripUserLinkUser=new ItripUserLinkUser();
            BeanUtils.copyProperties(itripUserLinkUser,vo);
            itripUserLinkUser.setModifiedBy(itripUser.getId());

            Integer integer=itripUserLinkUserService.itriptxModifyItripUserLinkUser(itripUserLinkUser);
            if (integer!=0){
                return DtoUtil.returnSuccess("修改联系人成功");
            }
            return DtoUtil.returnFail("修改联系人失败", "100421");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("新增常用联系人失败", "100411");
        }
    }

    /**
     *   查询常用联系人接口
     */
    @ApiOperation(value = "查询常用联系人接口")
    @RequestMapping(value = "/queryuserlinkuser", method = RequestMethod.POST, produces = "application/json")
    public Dto queryUserLinkUser(@RequestBody ItripSearchUserLinkUserVO vo, HttpServletRequest request){
        try {
            //1.验证token
            String token = request.getHeader("token");

            if (token==null || !redisUtils.exit(token)){
                return DtoUtil.returnFail("token失效，请重登录", "100000");
            }

            ItripUser itripUser = JSONObject.parseObject(redisUtils.getValue(token), ItripUser.class);

            List<ItripUserLinkUser> itripUserLinkUser=itripUserLinkUserService.getitripuserlinkUserbyUser(vo.getLinkUserName(),itripUser.getId());
            if (itripUserLinkUser!=null){
                return DtoUtil.returnDataSuccess(itripUserLinkUser);
            }
            return DtoUtil.returnFail("当前没有与这相关的联系人", "100401");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取常用联系人信息失败", "100401");
        }
    }

    /**
     *   删除常用联系人接口
     */
    @ApiOperation(value = "删除常用联系人接口")
    @RequestMapping(value = "/deluserlinkuser", method = RequestMethod.GET, produces = "application/json")
    public Dto deluserLinkUser(@RequestParam Integer[] ids, HttpServletRequest request){
        try {

            //1.验证token
            String token = request.getHeader("token");
            if (token==null || !redisUtils.exit(token)){
                return DtoUtil.returnFail("token失效，请重登录", "100000");
            }
            if (EmptyUtils.isEmpty(ids) || ids==null){
                return DtoUtil.returnFail("请选择要删除的常用联系人","100433");
            }
            //验证是否有待支付的订单用户
            boolean is=false;

            for (Integer id : ids) {
                if (itripHotelOrderService.getItripHotelOrderByIdAndStatus(id).size()==0){
                    itripUserLinkUserService.itriptxDeleteItripUserLinkUserById(Long.valueOf(id));
                }else {
                    is=true;
                }
            }
            if (is==true){
                return DtoUtil.returnFail(" 所选的常用联系人中有与某条待支付的订单关联的项,无关用户已删除", "100431");
            }
                return DtoUtil.returnSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("删除常用联系人失败", "100432");
        }
    }

    /**
     *   新增常用联系人接口
     */
    @ApiOperation(value = "新增常用联系人信息")
    @RequestMapping(value = "/adduserlinkuser", method = RequestMethod.POST, produces = "application/json")
    public Dto addUserLinkUser(@RequestBody ItripAddUserLinkUserVO vo, HttpServletRequest request){
        try {
            //1.验证token
            String token = request.getHeader("token");

            if (token==null || !redisUtils.exit(token)){
                return DtoUtil.returnFail("token失效，请重登录", "100000");
            }
            if (EmptyUtils.isEmpty(vo.getLinkIdCard()) || EmptyUtils.isEmpty(vo.getLinkIdCardType()) || EmptyUtils.isEmpty(vo.getLinkUserName())){
                return DtoUtil.returnFail("不能提交空，请填写常用联系人信息", "100412");
            }
            //转换为实体类
            ItripUser itripUser = JSONObject.parseObject(redisUtils.getValue(token), ItripUser.class);

            ItripUserLinkUser itripUserLinkUser=new ItripUserLinkUser();
            BeanUtils.copyProperties(itripUserLinkUser,vo);
            itripUserLinkUser.setCreatedBy(itripUser.getId());
            itripUserLinkUser.setUserId(itripUser.getId().intValue());

            Integer integer=itripUserLinkUserService.itriptxAddItripUserLinkUser(itripUserLinkUser);
            if (integer!=0){
                return DtoUtil.returnSuccess("新增联系人成功");
            }
            return DtoUtil.returnFail("新增常用联系人失败", "100411");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("新增常用联系人失败", "100411");
        }
    }

}
